package com.da.springcloud.controller;

import java.util.concurrent.TimeUnit;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JokerDa.
 * @date 2021/1/16 6:33
 * Description:
 */
@RestController
@Slf4j
public class FlowLimitController {

    /**
     * http://localhost:8401/testA
     */
    @GetMapping("/testA")
    public String testA() {
        //        try {
        //            TimeUnit.MILLISECONDS.sleep(800);
        //        } catch (InterruptedException e) {
        //            e.printStackTrace();
        //        }
        return "testA-----";
    }

    @GetMapping("/testB")
    public String testB() {
        //System.out.println(Thread.currentThread().getName() + "...testB ");
        return "testB   -----";
    }

    @GetMapping("/testD")
    public String testD() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("testD 测试RT");
        return "testD -----";
    }

    @GetMapping("/testException")
    public String testException() {
        log.info("testException 异常比例");
        int age = 10 / 0;
        return "testException -----";
    }

    @GetMapping("/testExceptionCount")
    public String testExceptionCount() {
        log.info("testExceptionCount 异常数");
        int age = 10 / 0;
        return "testExceptionCount -----";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "dealTestHotKey")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        //int age = 10 / 0;
        return "testHotKey -----";
    }

    public String dealTestHotKey(String p1, String p2, BlockException blockException) {
        return "dealTestHotKey---------";
    }
}
